package com.zmj.tl.database.taos.highvolume;

import cn.hutool.json.JSONUtil;

import java.util.Iterator;
import java.util.Map;

/**
 * @author zhangjp
 * @version 1.0
 * @title MockDataSource
 * @description
 * @create 2024/4/10 10:45
 */

public class MockDataSource implements Iterator {
    private String tbNamePrefix;
    private int tableCount;
    private long maxRowsPerTable = 1000000000L;
    private final Map<String, Object> map;
    // 100 milliseconds between two neighbouring rows.
    long startMs = System.currentTimeMillis() - maxRowsPerTable * 100;
    private int currentRow = 0;
    private int currentTbId = -1;

    // mock values
    String[] location = {"California.LosAngeles", "California.SanDiego", "California.SanJose", "California.Campbell", "California.SanFrancisco"};
    float[] current = {8.8f, 10.7f, 9.9f, 8.9f, 9.4f};
    int[] voltage = {119, 116, 111, 113, 118};
    float[] phase = {0.32f, 0.34f, 0.33f, 0.329f, 0.141f};

    public MockDataSource(String tbNamePrefix, int tableCount, Map<String, Object> map) {
        this.tbNamePrefix = tbNamePrefix;
        this.tableCount = tableCount;
        this.map = map;
    }

    @Override
    public boolean hasNext() {
        currentTbId += 1;
        if (currentTbId == tableCount) {
            currentTbId = 0;
            currentRow += 1;
        }
        return currentRow < maxRowsPerTable;
    }

    //    @Override
//    public String next() {
//        long ts = startMs + 100 * currentRow;
//        int groupId = currentTbId % 5 == 0 ? currentTbId / 5 : currentTbId / 5 + 1;
//        StringBuilder sb = new StringBuilder(tbNamePrefix + "_" + currentTbId + ","); // tbName
//        sb.append(ts).append(','); // ts
//        sb.append(current[currentRow % 5]).append(','); // current
//        sb.append(voltage[currentRow % 5]).append(','); // voltage
//        sb.append(phase[currentRow % 5]).append(','); // phase
//        sb.append(location[currentRow % 5]).append(','); // location
//        sb.append(groupId); // groupID
//        System.out.println(sb.toString());
//        return sb.toString();
//    }
    @Override
    public String next() {
        //Map metric = JSONUtil.toBean(map.get("metric").toString(), Map.class);
        Map tags =(Map) map.get("tags");
        StringBuilder sb = new StringBuilder(map.get("metric")+","+map.get("timestamp")+","+map.get("value")+","+tags.get("tname") + ","); // tbName
        sb.append(tags.get("receive_time")).append(','); // ts 4
        sb.append(tags.get("tenant_id")).append(','); // current 5
        sb.append(tags.get("device_name")).append(','); // current 6
        sb.append(tags.get("device_no")).append(','); // current 7
        sb.append(tags.get("device_type")).append(','); // current 8
        sb.append(tags.get("attribute_name")).append(','); // current 9
        sb.append(tags.get("message_id")).append(','); // current 10
        sb.append(tags.get("attribute_code")).append(','); // current 11
        sb.append(tags.get("quality")); // current 12
        return sb.toString();
    }
}